<template>
  <el-dialog title="角色信息" :visible.sync="tempDialogVisible" width="550px" :close-on-click-modal="false"
             @close="close">
    <el-form status-icon :model="tempData" :rules="rules" ref="ruleForm" label-width="100px">
      <el-form-item label="角色名称：" prop="name">
        <el-input size="medium" v-model="tempData.name"></el-input>
      </el-form-item>
      <el-form-item label="角色权限：">
        <tree :setting="setting" :nodes="treeDatas" @onCreated="handleCreated"/>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="submitForm('ruleForm')">保存</el-button>
    </span>
  </el-dialog>
</template>

<script>
import tree from 'vue-giant-tree'
import { getMenu, add, edit } from '@/api/role'

export default {
  name: 'RoleAddEdit',
  components: { tree },
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    data: {
      type: Object,
      default: {
        id: null,
        name: null,
        roles: []
      }
    }
  },
  data() {
    return {
      tempDialogVisible: false,
      tempData: {
        id: null,
        name: null,
        roles: []
      },
      //ztree数据
      treeDatas: [],
//ztree对象
      ztreeObj: null,
//ztree配置，参照ztree官网的配置
      setting: {
        check: {
          //树是否带复选框或者单选框
          enable: true
        },
        data: {
          simpleData: {
            //是否使用简单数据模式
            enable: true,
            //树节点id，一般是后台数据库主键
            idKey: "id",
            //父级id
            pIdKey: "pid",
            rootPId: "0"
          }
        },
        //ztree回调函数
        callback: {
          //树选择事件
          onCheck: this.ztreeOnCheck
        }
      },
      rules: {
        name: [
          { required: true, message: '请输入角色名称', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    dialogVisible: function(val) {
      this.tempDialogVisible = val
    },
    data: function(val) {
      this.tempData = val
      if (this.tempDialogVisible) {
        this.initRole()
      }
    }
  },
  mounted() {

  },
  methods: {
    initRole() {
      getMenu(this.tempData.id).then(res => {
        this.treeDatas = res.data.menu || []
      })
    },
    selectDefault() {

    },
    handleCreated: function(ztreeObj) {
      this.ztreeObj = ztreeObj
      this.ztreeObj.expandAll(true)
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let checked = this.ztreeObj.getCheckedNodes(true)
          let subData = {
            name: this.tempData.name,
            roles: []
          }
          if (checked && checked.length > 0) {
            checked.forEach(item => {
              subData.roles.push(item.id)
            })
          }
          if (this.tempData.id > 0) {
            subData.id = this.tempData.id
            edit(subData).then(res => {
              this.$message.success('修改成功')
              this.close()
            })
          } else {
            add(subData).then(res => {
              this.$message.success('添加成功')
              this.close()
            })
          }

        } else {
          return false
        }
      })
    },
    close() {
      this.ztreeObj.checkAllNodes(false)
      this.$refs['ruleForm'].clearValidate()
      this.$emit('close')
    }
  }
}
</script>

<style scoped>

</style>
